package com.ems.service.impl.user;

import com.ems.dao.supers.action.UserGroupDao;
import com.ems.pojo.Invigilate.room.RoomUser;
import com.ems.pojo.permission.UserGroup;
import com.ems.service.Invigilate.room.RoomUserService;
import com.ems.service.Invigilate.room.TestRoomService;
import com.ems.service.Invigilate.room.TestScoreResultService;
import com.ems.service.supers.action.UserGroupService;
import com.ems.service.user.UserService;
import com.ems.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.ems.pojo.user.User;
import com.ems.dao.user.UserDao;

import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;

	@Autowired
	private UserGroupDao userGroupDao;

    @Autowired
    @Qualifier("UserGroupService")
    private UserGroupService userGroupService;

    @Autowired
    @Qualifier("roomUserService")
    private RoomUserService roomUserService;

    @Autowired
    @Qualifier("testRoomService")
    private TestRoomService testRoomService;

    @Autowired
    @Qualifier("testScoreResultService")
    private TestScoreResultService testScoreResultService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public User userLogin(User user) {
        User user1 = userDao.getUserLoginInfo(user);
        return user1;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public int userRegister(User user) {
        //根据用户名查询该用户名是否存在
        int count = userNameCount(user.getUser_name());
        if(count > 0){
            return 1;
        }else{

            //判断该账户的邮箱是否有被绑定
            User user1 = getUserByEmail(user.getUser_email());
            if(user1 != null && user1.getId() != 0){
                return -1;
            }else{
                //进行注册
                user.setUser_time(Tools.getTime());

                userDao.userRegister(user);

                //为该用户添加角色
                userGroupDao.addUserGroup(new UserGroup(user.getId(),user.getUser_group_type()));
                return 0;
            }
        }
    }

    /**
     * 根据角色查询所有用户
     * @param gr_id
     * @return
     */
    @Override
    public List<User> getAllUserByGroupId(int gr_id) {
        return userDao.getAllUserByGroupId(1);
    }

    /**
     * 根据用户id修改用户信息
     * @param user
     */
    @Override
    public void updateUserByUserId(User user) {
       userDao.updateUserByUserId(user);
    }

    /**
     * 修改用户的密码
     * @param user
     */
    @Override
    public void updateUserPasswordById(User user) {
        userDao.updateUserPasswordById(user);
    }

    /**
     * 根据条件查询用户信息
     * @param map
     * @return
     */
    @Override
    public List<User> findUserByCondition(Map map) {
        return userDao.findUserByCondition(map);
    }

    /**
     * 添加用户
     * @param user
     */
    @Override
    public int addUser(User user) {

        user.setUser_group_type(user.getGroup().getGr_id());

        //添加用户信息
        //userDao.addUser(user);

        //查询用户信息
        //User user1 = userDao.getUserLoginInfo(user);

        //添加角色信息
        //userGroupDao.addUserGroup(new UserGroup(user1.getId(),user.getGroup().getGr_id()));


        return userRegister(user);
    }

    /**
     * 删除用户
     * @param list
     */
    @Override
    public void deleteUser(List<User> list) {
        if(list != null && list.size() > 0){
            roomUserService.deleteRoomUserById(list);

            testScoreResultService.deleteTestScoreResultById(list);

            User user = null;
            for(int i = 0;i < list.size();i ++){
                user = list.get(i);

                //删除用户（如果是教师角色）
                testRoomService.deleteTestRoolById(user);

                //删除用户的角色信息
                userGroupService.deleteUserGroupById(user.getId());

                //删除用户
                userDao.deleteUserById(user.getId());
            }
        }
    }

    //根据id查询指定的用户
    @Override
    public User getUserById(int id) {
        return userDao.getUserById(id);
    }


    @Override
    public List<User> getUserInfoByListId(List<RoomUser> roomUserList,Map<String, Object> map) {
        return userDao.getUserInfoByListId(roomUserList,map);
    }

    /**
     * 查询指定用户名下面用户的数量
     * @param userName
     * @return
     */
    @Override
    public int userNameCount(String userName) {
        return userDao.userNameCount(userName);
    }

    @Override
    public User getUserByEmail(String user_email) {
        return userDao.getUserByEmail(user_email);
    }
}
